(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.monitor.wfFault.listController', listController);
	listController.$inject = ['$rootScope', '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ajaxServiceFactory'];
	function listController($rootScope, $scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("wffault");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
		    getFilterDatas:{
		    	url : 'do/cda/tools/monitor/wfFault/dataFilter',
				method : 'get'
		    }
		});
		
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ajaxService.getFilterDatas().then(function(response) {
				if(response && response.data){
					ctrl.wferrCatFilter=response.data.wferrCatStatus;
				}
				
				ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
				ctrl.gridOptions.data = 'vm.searchCriteria.list';
				ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
				ctrl.search();
			});
		};

		ctrl.reprocess = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.reprocess(ctrl.selectedRows[0]).then(function(response) {
				ctrl.search();
			}, angular.noop);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
			$rootScope.initAuditOrNoteParams("","");
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);